package com.steven.android40_weathermvp.presenter;

import com.steven.android40_weathermvp.bean.WeatherBean;
import com.steven.android40_weathermvp.model.IWeatherModel;
import com.steven.android40_weathermvp.model.WeatherModelImpl;
import com.steven.android40_weathermvp.view.interfaces.IWeatherView;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;


public class WeatherPresenterImpl implements IWeatherPresenter {

    private IWeatherModel mWeatherModel = null;
    private IWeatherView mWeatherView = null;

    public WeatherPresenterImpl(IWeatherView _weatherView) {
        mWeatherModel = new WeatherModelImpl();
        this.mWeatherView = _weatherView;
    }

    @Override
    public void loadWeatherData(String city) {

        mWeatherModel.loadWeatherData(city)
                .subscribeOn(Schedulers.io())// 在非UI线程中执行
                .observeOn(AndroidSchedulers.mainThread())// 在UI线程中执行结果
                .subscribe(new Subscriber<WeatherBean>() {
                    @Override
                    public void onCompleted() {
                        mWeatherView.hideProgress();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mWeatherView.showErrorInfo("获取天气数据失败");
                        mWeatherView.hideProgress();
                    }

                    @Override
                    public void onNext(WeatherBean weatherBean) {
                        mWeatherView.loadWeatherData(weatherBean);
                    }

                    @Override
                    public void onStart() {
                        super.onStart();
                        mWeatherView.showProgress();
                    }
                });


    }


}
